<?php

namespace app\admin\model\union;
use app\admin\model\channel\AppChannel;
use think\Model;

class Guild extends Model
{
    protected $table = 't_guild_manage';
    protected $connection="database.guild";
    protected $resultSetType = 'collection';
    public static function getGuild(){
        $data = self::field('id,name')->where('status',1)->select()->toArray();
        array_push($data, ['id'=>0,'name'=>'无']);
        return array_column($data,'name','id');
    }
    //查询所有的推荐人
    public static function getChildGuild($guid_id = 0){
        if ($guid_id == 0){
            $data = self::field('id,name')->where('status',1)->select()->toArray();
//            if (!empty($guid_id))$data = $data->where('parent_guild_id',$guid_id);
//            $data = $data->select()->toArray();
        }else{
            $data = self::field('id,name')->where('status',1);
            if (!empty($guid_id))$data = $data->where('parent_guild_id',$guid_id);
            $data = $data->select()->toArray();
        }

        return array_column($data,'name','id');
    }
    //查询自己和推荐人
    public static function getChildGuild2($guid_id = 0){
        if ($guid_id == 0){
            $data = self::field('id,name')->where('status',1)->select()->toArray();
        }else{
            $data = self::field('id,name')->where('id',$guid_id)->whereOr('parent_guild_id',$guid_id)->select()->toArray();
        }
        return array_column($data,'name','id');
    }

    //查询所有的推荐人
    public static function getGuildChannel($guild_id = 0)
    {
        $channels = self::field('channel')->where('channel','<>','');
        if($guild_id){
            $channels = $channels->where('id',$guild_id);
        }
        $channels =  $channels->select()->toArray();
        if(!$channels) return [];
        $channel_arr = [];
        foreach ($channels as $v)
        {
            if(stripos($v['channel'],',')){
                $channel_arr = array_merge($channel_arr,explode(',',$v['channel']));
            }else{
                $channel_arr[] = $v['channel'];
            }
        }
        $channel_arr = array_unique(array_values($channel_arr));
        $data = AppChannel::field('channel,channel_name')->where('channel','in',$channel_arr)->select()->toArray();
        return array_column($data,'channel_name','channel');
    }
}